// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// G [author] rhyme_lph
// L [email]  rhymelph@gmail.com
// P [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/entities/transaction_type.dart';
import 'package:finance_app/src/widgets/bottom_sheet/category_bottom_sheet.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/search_input_field.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/payment/select_transaction_purpose_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

export 'package:finance_app/src/entities/transaction_type.dart';

@RouterMeta(paramName: "selectTransactionPurposePage")
class SelectTransactionPurposePage extends StatefulWidget {
  final SettingDataCategory select;
  final bool isCashOut;
  const SelectTransactionPurposePage({Key key, this.select, this.isCashOut})
      : super(key: key);

  @override
  _SelectTransactionPurposePageState createState() =>
      _SelectTransactionPurposePageState();
}

class _SelectTransactionPurposePageState
    extends State<SelectTransactionPurposePage>
    with
        PagePresenter<SelectTransactionPurposePage,
            SelectTransactionPurposeProvider> {
  @override
  void initState() {
    provider.currentSelect = widget.select;
    provider.isCashOut = widget.isCashOut;
    super.initState();
  }

  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.transaction_purpose);

  @override
  // TODO: implement buildEmptyWidget
  Widget get buildEmptyWidget => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            Expanded(child: super.buildEmptyWidget),
            SearchInputField(
              key: ValueKey('Search Input'),
            ),
            Expanded(child: super.buildEmptyWidget),
          ],
        ),
      );

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: CategoryWidget(
          isSinglePage: true,
          categories: provider.data,
          recentIds: provider.recentIds,
          currentSelectCode: provider.currentSelect,
          selectCallBack: provider.onChangeSelect,
        ),
      );

  Widget _buildItems(BuildContext context, int index) {
    final item = provider.data[index];
    return PressedOpacityButton(
      onPressed: () => provider.onChangeSelect(item),
      padding: EdgeInsets.symmetric(
        vertical: getDp(10),
        horizontal: getDp(34),
      ),
      child: listBuilder<String>(
        selector: (_, __) => __.currentSelect?.key,
        builder: (BuildContext context, String value, Widget child) =>
            NormalRadio(
          title: item.name,
          isSelect: value == item.key,
          isFix: true,
        ),
      ),
    );
  }
}
